package meta.api.web.common.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import meta.api.web.directory.businessinfo.DirInfo;
import meta.api.web.file.businessinfo.FileInfo;
import meta.api.web.file.businessinfo.MetadataInfo;
import meta.api.web.file.presentation.form.MetadataForm;

@Repository
public class BusiCmnDao {

    @Autowired
    private SqlSessionTemplate sqlSession;

    private static final String NAMESPACE = "meta.api.web.common.dao.busiCmn.";

    public int getFileTotal(MetadataInfo inputMetadata, String userId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("type", inputMetadata.getType());
        params.put("directory", inputMetadata.getDirectory());
        params.put("allChildren", inputMetadata.isAllChildren());
        params.put("userId", userId);
        return sqlSession.selectOne(NAMESPACE + "getFileTotal", params);
    }

    public List<FileInfo> getFileList(MetadataInfo inputMetadata, String userId, Integer current, Integer pageSize) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("type", inputMetadata.getType());
        params.put("directory", inputMetadata.getDirectory());
        params.put("allChildren", inputMetadata.isAllChildren());
        params.put("userId", userId);
        if (current != null && pageSize != null) {
            params.put("current", pageSize * (current - 1));
            params.put("pageSize", pageSize);
        } else {
            params.put("current", null);
            params.put("pageSize", null);
        }
        return sqlSession.selectList(NAMESPACE + "getFileList", params);
    }

    public void saveFile(FileInfo fileInfo, byte[] fileContent) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("fileInfo", fileInfo);
        params.put("fileContent", fileContent);
        sqlSession.insert(NAMESPACE + "saveFile", params);
    }

    public FileInfo getFileByFilename(String filename, String userId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("filename", filename);
        params.put("userId", userId);
        return sqlSession.selectOne(NAMESPACE + "getFileByFilename", params);
    }

    public boolean checkIsDirOwner(String id, String userId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);
        params.put("userId", userId);
        Integer result = sqlSession.selectOne(NAMESPACE + "checkIsDirOwner", params);
        return result > 0 ? true : false;
    }

    /**
     * 检证文件是否存在
     * 
     * @param filename 文件名
     * @param existFlg null :文件记录存在，忽略文件删除日期是否为NULL<br>
     *                 true :文件逻辑存在，文件删除日期为NULL<br>
     *                 false:文件逻辑删除，文件删除日期不为NULL
     * @return 存在的场合返回true，否则返回false。
     */
    public boolean checkIsFileExisted(String filename, Boolean existFlg) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("filename", filename);
        params.put("existFlg", existFlg);
        Integer result = sqlSession.selectOne(NAMESPACE + "checkIsFileExisted", params);
        return result > 0 ? true : false;
    }

    public boolean checkIsFileOwner(String filename, String userId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("filename", filename);
        params.put("userId", userId);
        Integer result = sqlSession.selectOne(NAMESPACE + "checkIsFileOwner", params);
        return result > 0 ? true : false;
    }

    public boolean checkIsDataOwner(String id, String userId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);
        params.put("userId", userId);
        Integer result = sqlSession.selectOne(NAMESPACE + "checkIsDataOwner", params);
        return result > 0 ? true : false;
    }

    public void updateFiles(List<String> filenames, MetadataForm metadataForm, String userId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("filenames", filenames);
        params.put("name", metadataForm.getName());
        params.put("tags", metadataForm.getTags());
        params.put("shared", metadataForm.getShared());
        params.put("directory", metadataForm.getDirectory());
        params.put("remarks", metadataForm.getRemarks());
        params.put("type", metadataForm.getType());
        params.put("imgName", metadataForm.getImageName());
        params.put("imgUrl", metadataForm.getImageUrl());
        params.put("userId", userId);
        sqlSession.update(NAMESPACE + "updateFiles", params);
    }

    public void deleteFilesByFilenames(List<String> filenames, String userId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("filenames", filenames);
        params.put("userId", userId);
        sqlSession.delete(NAMESPACE + "deleteFilesByFilenames", params);
    }

    public void updateFilesDeletedByFilenames(List<String> filenames, String userId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("filenames", filenames);
        params.put("userId", userId);
        sqlSession.update(NAMESPACE + "updateFilesDeletedByFilenames", params);
    }

    public void updateFilesNotDeletedByFilenames(List<String> filenames, String userId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("filenames", filenames);
        params.put("userId", userId);
        sqlSession.update(NAMESPACE + "updateFilesNotDeletedByFilenames", params);
    }

    public boolean checkExistDir(String userId, String type, String fullpath) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", userId);
        params.put("type", type);
        params.put("fullpath", fullpath);
        Integer result = sqlSession.selectOne(NAMESPACE + "checkExistDir", params);
        return result > 0 ? true : false;
    }

    public void addDirInfo(String userId, String type, String fullpath) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", userId);
        params.put("type", type);
        params.put("fullpath", fullpath);
        sqlSession.insert(NAMESPACE + "addDirInfo", params);
    }

    public void updateDirInfo(String userId, String type, String oldFullpath, String newFullpath) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", userId);
        params.put("type", type);
        params.put("oldFullpath", oldFullpath);
        params.put("newFullpath", newFullpath);
        sqlSession.update(NAMESPACE + "updateDirInfo", params);
    }

    public DirInfo getDirInfo(String id, String userId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);
        params.put("userId", userId);
        return sqlSession.selectOne(NAMESPACE + "getDirInfoById", params);
    }

    public DirInfo getDirInfo(String userId, String type, String fullpath) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", userId);
        params.put("type", type);
        params.put("fullpath", fullpath);
        return sqlSession.selectOne(NAMESPACE + "getDirInfoByTypeAndFullpath", params);
    }

    public int getDirInfoTotal(String userId, String type, String fullpath) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", userId);
        params.put("type", type);
        params.put("fullpath", fullpath);
        return sqlSession.selectOne(NAMESPACE + "getDirInfoTotal", params);
    }

    public List<DirInfo> getDirInfoList(String userId, String type, String fullpath) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", userId);
        params.put("type", type);
        params.put("fullpath", fullpath);
        return sqlSession.selectList(NAMESPACE + "getDirInfoList", params);
    }

    public void deleteDirInfo(String id, String userId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("id", id);
        params.put("userId", userId);
        sqlSession.delete(NAMESPACE + "deleteDirInfoById", params);
    }

    public void deleteDirInfo(String userId, String type, List<String> fullpaths) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("userId", userId);
        params.put("type", type);
        params.put("fullpaths", fullpaths);
        sqlSession.delete(NAMESPACE + "deleteDirInfoByTypeAndFullpaths", params);
    }
}
